package com.joymart.common.abstraction;

import com.joymart.common.error.handling.JoymartBusinessException;
import com.joymart.common.utils.SecurityUtils;
import com.joymart.user.model.CachedUser;
import com.joymart.user.service.UserCacheManager;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.function.Predicate;

public abstract class FlowService<T> {

    @Autowired
    private UserCacheManager userCacheManager;

    /**
     * 检查人员是否具有权限进行操作
     * @param t
     * @param predicates 多个条件只需满足一个， 换言之 or 的关系
     */
    public void checkPerson(T t, Predicate<T>... predicates){

        boolean legal = Arrays.stream(predicates).anyMatch(predicate -> predicate.test(t));
        if(!legal){
            throw new JoymartBusinessException("您无权进行此项操作");
        }
    }


    public CachedUser getCurrentUser(){
        String userId = SecurityUtils.getCurrentUserId();
        return userCacheManager.get(userId);
    }



}
